package com.itheima.controller;


import com.itheima.common.DefaultException;
import com.itheima.common.R;
import com.itheima.config.ThreadPoolConfig;
import com.itheima.dto.CommentDto;
import com.itheima.service.CommentService;
import com.itheima.vo.CommentVo;
import com.itheima.vo.comment.CommentChildVo;
import com.itheima.vo.comment.CommentParentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.concurrent.Semaphore;

/**
 * (Comment)表控制层
 *
 * @author makejava
 * @since 2022-10-01 22:01:14
 */
@RestController
@RequestMapping("comment")
@Api(tags = "评论api")
@Validated
public class CommentController {

    @Resource
    private CommentService commentService;

    /**
     * 发表评论
     *
     * @param commentDto
     * @return
     */
    @ApiOperation(value = "发表评论api", notes = "示例：" +
            "{\n" +
            "    \"messageId\": 10,\n" +
            "    \"content\": \"对评论1的回复\",\n" +
            "    \"parentCommentId\": 1\n" +
            "}")
    @PostMapping("/sendComment")
    public R<String> sendComment(@Valid @RequestBody CommentDto commentDto) throws Exception {
        return R.success(commentService.sendComment(commentDto));
    }


    /**
     * 删除评论
     *
     * @param commentId
     * @return
     */
    @ApiOperation(value = "删除评论api")
    @DeleteMapping("/deleteComment")
    public R<String> deleteComment(Integer commentId) throws Exception {
        return R.success(commentService.deleteComment(commentId));
    }

    /**
     * 展示评论
     *
     * @param messageId
     * @return
     */
    @ApiOperation(value = "展示评论api")
    @GetMapping("/showComment")
    public R<List<CommentVo>> showComment(Integer messageId) {
        return R.success(commentService.showComment(messageId));
    }

    /**
     * 查看父级评论
     *
     * @param messageId 信息id
     * @return 父评论VO
     */
    @ApiOperation(value = "查看父级评论")
    @GetMapping("/parentComment")
    public R<List<CommentParentVo>> parentComment(@NotNull(message = "信息id不能为空") Integer messageId) {
        return R.success(commentService.parentComment(messageId));
    }

    /**
     * 查看子评论
     *
     * @param commentId 评论id
     * @return 子评论Vo
     */
    @ApiOperation(value = "查看子评论")
    @GetMapping("/childComment")
    public R<List<CommentChildVo>> childComment(@NotNull(message = "评论id不能为空") Integer commentId) throws DefaultException {
        return R.success(commentService.childComment(commentId));
    }

    @Resource
    private ThreadPoolConfig threadPoolConfig;

    static Integer i = 0;

    @GetMapping("doIt")
    public void doIt() {
        Semaphore semaphore = new Semaphore(2);
        System.out.println(i);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    while (i <= 100) {
                        if (i % 2 == 0) {
                            semaphore.acquire();
                            System.out.println(this);
                            System.out.println(i++);
                        } else {
                            semaphore.release();
                        }
                    }

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }


            }
        };
        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                try {
                    while (i <= 100) {

                        if (i % 2 == 1) {
                            semaphore.acquire();
                            System.out.println(this);
                            Thread.sleep(2);
                            System.out.println(i++);
                        } else {
                            semaphore.release();
                        }

                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }


            }
        };
        threadPoolConfig.asyncExecutor().execute(runnable);
        threadPoolConfig.asyncExecutor().execute(runnable2);
    }

}

